﻿using B.S.Domain.Date;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;

namespace B.S.Data.MES.API.Applications.CreateTimeApplication
{
    public class CreateTimeCommandHandlers : IRequestHandler<CreateTimeCommand, APIRequst<int>>
    {
        public CreateTimeCommandHandlers(IBaseRepository<TimeClass> tiemClassRepository)
        {
            TiemClassRepository = tiemClassRepository;
        }

        public IBaseRepository<TimeClass> TiemClassRepository { get; }

        public Task<APIRequst<int>> Handle(CreateTimeCommand request, CancellationToken cancellationToken)
        {
            int currentYear = DateTime.Now.Year;
            List<TimeClass>? path = TiemClassRepository.GetAll().ToList();
            DateTime startDate = new DateTime(currentYear, 1, 1);
            DateTime endDate = new DateTime(currentYear, 12, 31);

            int tidCounter = 1;
            for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
            {
                TimeClass dateInfo = new TimeClass
                {
                    Tid = tidCounter++,
                    Years = date.Year,
                    Months = date.Month,
                    // 这里简单判断是否为白天，实际可根据具体需求修改
                    Days = date.Day,
                    IsDay = true
                };
                TiemClassRepository.Add(dateInfo);
            }

            return Task.FromResult(new APIRequst<int> { Code=RequstCode.成功,Msg="",Data=1});
        }
    }
}
